﻿using System;
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Interactivity;
using DevHub.Lib;

namespace DevHub.WindowsApp.Pages.Converter;

public partial class TranslateView : UserControl
{
    private static Dictionary<string, string> From => new()
    {
        { "自动", "AUTO" },
        { "中文", "zh-CHS" },
        { "英语", "en" },
        { "日语", "ja" },
        { "韩语", "ko" },
        { "法语", "fr" },
        { "阿拉伯语", "ar" },
        { "德语", "de" },
        { "俄语", "ru" },
        { "荷兰语", "nl" },
        { "葡萄牙语", "pt" },
        { "泰语", "th" },
        { "西班牙语", "es" },
        { "意大利语", "it" },
        { "越南语", "vi" },
        { "印度尼西亚语", "id" }
    };

    public TranslateView()
    {
        InitializeComponent();
        ContextCombo.ItemsSource = TranslateCombo.ItemsSource = From.Keys;
        ContextCombo.SelectedItem = TranslateCombo.SelectedItem = "自动";
    }

    [Obsolete("Obsolete")]
    private void ContextTextChanged(object? sender, TextChangedEventArgs e)
    {
        if (string.IsNullOrEmpty(ContextBox.Text) || ContextCombo.SelectedItem is not string comboSelected ||
            TranslateCombo.SelectedItem is not string translateSelected)
            return;

        TranslateBox.Text = Translate.TranslateFromYOUDAO(ContextBox.Text, From[comboSelected],
            From[translateSelected]);
    }

    private void CopyContextClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(ContextBox.Text)) return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(ContextBox.Text);
    }

    private void ClearContextClick(object? sender, RoutedEventArgs e)
    {
        ContextBox.Text = "";
    }

    private void CopyTranslateClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(TranslateBox.Text)) return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(TranslateBox.Text);
    }

    private void ClearTranslateClick(object? sender, RoutedEventArgs e)
    {
        TranslateBox.Text = "";
    }
}